<template>
  <el-dialog
    :title="!dataForm.id ? '新增' : '修改'"
    :close-on-click-modal="false"
    :visible.sync="visible"
  >
    <el-form
      :model="dataForm"
      :rules="dataRule"
      ref="dataForm"
      @keyup.enter.native="dataFormSubmit()"
      label-width="80px"
    >
      <el-form-item label="学生邮箱" prop="studentEmail">
        <el-input
          v-model="dataForm.studentEmail"
          placeholder="学生邮箱"
        ></el-input>
      </el-form-item>
      <el-form-item label="学生姓名" prop="studentName">
        <el-input
          v-model="dataForm.studentName"
          placeholder="学生姓名"
        ></el-input>
      </el-form-item>
      <el-form-item label="用户名" prop="studentUsername">
        <el-input
          v-model="dataForm.studentUsername"
          placeholder="用户名"
        ></el-input>
      </el-form-item>
      <el-form-item label="头像" prop="studentAvatar">
        <el-upload
          action="http://localhost:8888/api/minio/upload"
          list-type="picture-card"
          :on-preview="handlePictureCardPreview"
          :on-remove="handleRemove"
          :on-success="handleUploadSuccess"
        >
          <i class="el-icon-plus"></i>
        </el-upload>
        <el-dialog :visible.sync="dialogVisible">
          <img width="100%" :src="dataForm.studentAvatar" alt="" />
        </el-dialog>

        <!-- <img :src="dataForm.studentAvatar" min-width="70" height="70" />
        <el-upload
          v-model="dataForm.studentAvatar"
          class="upload-demo"
          action="http://localhost:8888/api/minio/upload"
          :on-preview="handlePreview"
          :on-remove="handleRemove"
          :before-remove="beforeRemove"
          :on-success="handleUploadSuccess"
          :file-list="fileList.url"
          list-type="picture"
        >
          <el-button size="small" type="primary">点击上传</el-button>
          <div slot="tip" class="el-upload__tip">
            只能上传jpg/png文件，且不超过500kb
          </div>
        </el-upload> -->
      </el-form-item>

      <el-form-item label="出生日期" prop="studentAge">
        <el-date-picker
          v-model="dataForm.studentAge"
          type="date"
          placeholder="选择日期"
        >
        </el-date-picker>
      </el-form-item>
      <el-form-item label="性别">
        <el-radio-group v-model="dataForm.studentGender">
          <el-radio label="男"></el-radio>
          <el-radio label="女"></el-radio>
        </el-radio-group>
      </el-form-item>
      <el-form-item label="个性签名" prop="studentIntroduction">
        <el-input
          v-model="dataForm.studentIntroduction"
          placeholder="个性签名"
        ></el-input>
      </el-form-item>
      <el-form-item label="学历等级" prop="levelName">
        <el-select v-model="dataForm.levelName" placeholder="请选择">
          <el-option
            v-for="item in options"
            :key="item.id"
            :label="item.levelName"
            :value="item.id"
          >
          </el-option>
        </el-select>
      </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="close">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
export default {
  data() {
    return {
      visible: false,
      dataForm: {
        id: 0,
        studentEmail: "",
        studentName: "",
        studentUsername: "",
        studentPassword: "",
        studentAvatar: "",
        studentAge: "",
        studentIntroduction: "",
        levelName: "",
        levelId: "",
        createTime: "",
        updateTime: "",
        isDelete: "",
        studentGender: "",
      },
      dialogImageUrl: "",
      dialogVisible: false,
      fileList: [
        {
          name: "",
          url: "",
        },
      ],

      flag: true,
      options: [],
      dataRule: {
        studentEmail: [
          { required: true, message: "学生邮箱不能为空", trigger: "blur" },
        ],
        studentName: [
          { required: true, message: "学生姓名不能为空", trigger: "blur" },
        ],
        studentUsername: [
          { required: true, message: "姓名不能为空", trigger: "blur" },
        ],
        studentPassword: [
          { required: true, message: "密码不能为空", trigger: "blur" },
        ],
        studentAvatar: [
          { required: true, message: "头像不能为空", trigger: "blur" },
        ],
        studentAge: [
          { required: true, message: "学生年龄不能为空", trigger: "blur" },
        ],
        studentIntroduction: [
          { required: true, message: "个性签名不能为空", trigger: "blur" },
        ],
        levelName: [
          { required: true, message: "学历等级不能为空", trigger: "blur" },
        ],
        createTime: [
          { required: false, message: "创建时间不能为空", trigger: "blur" },
        ],
        updateTime: [
          { required: false, message: "修改时间不能为空", trigger: "blur" },
        ],
      },
    };
  },
  created() {
    this.updateFlag();
  },
  methods: {
    init(id) {
      this.getStudentLevel();
      this.dataForm.id = id || 0;
      this.visible = true;
      this.updateFlag();
      this.$nextTick(() => {
        this.$refs["dataForm"].resetFields();
        if (this.dataForm.id) {
          this.$http({
            url: this.$http.adornUrl(
              `/student/student/findStudentDetailById/${this.dataForm.id}`
            ),
            method: "get",
            params: this.$http.adornParams(),
          }).then(({ data }) => {
            if (data && data.code === 0) {
              console.log(data.detail);
              console.log(typeof data.detail.levelName, "111");
              console.log(typeof data.detail.levelName, "111");
              console.log(data.detail.levelName, "sad");

              this.dataForm.studentEmail = data.detail.studentEmail;
              this.dataForm.studentName = data.detail.studentName;
              this.dataForm.studentUsername = data.detail.studentUsername;
              this.dataForm.studentPassword = data.detail.studentPassword;
              this.dataForm.studentAvatar = data.detail.studentAvatar;
              this.dataForm.studentAge = data.detail.studentAge;
              this.dataForm.studentIntroduction =
                data.detail.studentIntroduction;
              this.dataForm.levelName = data.detail.levelName;
              this.dataForm.createTime = data.detail.createTime;
              this.dataForm.updateTime = data.detail.updateTime;
              this.dataForm.isDelete = data.detail.isDelete;
              this.dataForm.studentGender = data.detail.studentGender;
            }
          });
        }
      });
    },
    // 表单提交
    dataFormSubmit() {
      this.$refs["dataForm"].validate((valid) => {
        // if (typeof this.dataForm.levelName === "string") {
        //   for (var i = 0; i < this.options.length; i++) {
        //     if (
        //       this.dataForm.id &&
        //       this.dataForm.levelName === this.options[i].levelName
        //     ) {
        //       this.dataForm.levelName = this.options[i].id;
        //     }
        //     console.log(this.dataForm.levelName, "sadas");
        //   }
        // }
        if (valid) {
          this.$http({
            url: this.$http.adornUrl(
              `/student/student/${!this.dataForm.id ? "save" : "update"}`
            ),
            method: "post",
            data: this.$http.adornData({
              id: this.dataForm.id || undefined,
              studentEmail: this.dataForm.studentEmail,
              studentName: this.dataForm.studentName,
              studentUsername: this.dataForm.studentUsername,
              studentPassword: this.dataForm.studentPassword,
              studentAvatar: this.dataForm.studentAvatar,
              studentAge: new Date(this.dataForm.studentAge),
              studentIntroduction: this.dataForm.studentIntroduction,
              levelName: this.dataForm.levelName,
              createTime: this.dataForm.createTime,
              updateTime: this.dataForm.updateTime,
              isDelete: this.dataForm.isDelete,
              studentGender: this.dataForm.studentGender,
            }),
          }).then(({ data }) => {
            if (data && data.code === 0) {
              this.$message({
                message: "操作成功",
                type: "success",
                duration: 1500,
                onClose: () => {
                  this.visible = false;
                  console.log("sadas");
                  this.$emit("refreshDataList");

                  this.fileList = [];
                },
              });
            } else {
              this.$message.error(data.msg);
            }
          });
        }
      });
    },
    // 获取学生学历等级数据
    getStudentLevel() {
      this.$http({
        url: this.$http.adornUrl(
          "/student/studentlevel/findStudentLevelNameList"
        ),
        method: "get",
      }).then(({ data }) => {
        if (data && data.code === 0) {
          console.log(data.levelNameList);
          this.options = data.levelNameList;
        } else {
          this.options = [];
        }
      });
    },

    handlePreview(file) {
      console.log(file);
    },

    handleRemove(file, fileList) {
      console.log(file, fileList);
    },
    handlePictureCardPreview(file) {
      console.log(file);
      this.dataForm.studentAvatar = file.url;
      this.dialogImageUrl = file.url;
      this.dialogVisible = true;
    },

    handleUploadSuccess(response, res, file) {
      if (file.length > 1) {
        this.$message.error("最多只能上传一张图片");
      } 
    },
    updateFlag() {
      if (this.dataForm.studentAvatar !== "") {
        this.flag = true;
      } else {
        this.flag = false;
      }
      console.log(this.flag);
    },
    beforeRemove(file, fileList) {
      return this.$confirm(`确定移除 ${file.name}？`);
    },
    close() {
      this.visible = false;
      this.handleRemove(this.fileList);
    },
  },
};
</script>
